package com.meessiom.train.crm.collection.controller;

import com.meessiom.train.crm.collection.model.Collection;
import com.meessiom.train.crm.collection.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin("*")
@RestController
@RequestMapping("/api/collections")
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    @GetMapping
    public List<Collection> getAllCollections() {
        return collectionService.getAllCollections();
    }

    @PostMapping
    public Collection createCollection(@RequestBody Collection collection) {
        return collectionService.createCollection(collection);
    }

    @GetMapping("{id}")
    public ResponseEntity<Collection> getCollectionById(@PathVariable long id) {
        return ResponseEntity.ok(collectionService.getCollectionById(id));
    }

    @PutMapping("{id}")
    public ResponseEntity<Collection> updateCollection(@PathVariable long id, @RequestBody Collection collectionDetails) {
        return ResponseEntity.ok(collectionService.updateCollection(id, collectionDetails));
    }

    @DeleteMapping("{id}")
    public ResponseEntity<HttpStatus> deleteCollection(@PathVariable long id) {
        collectionService.deleteCollection(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    // 高级搜索接口
    @GetMapping("/search")
    public List<Collection> searchCollections(
            @RequestParam(value = "orderId", required = false) String orderId,
            @RequestParam(value = "approvalStatus", required = false) String approvalStatus,
            @RequestParam(value = "overdueStatus", required = false) String overdueStatus,
            @RequestParam(value = "paymentMethod", required = false) String paymentMethod) {

        return collectionService.searchCollections(
                orderId, approvalStatus, overdueStatus, paymentMethod
        );
    }
}